<div class="system-messages">
    <ul>
        <?php
        $messages = $this->messages;
        foreach ($messages as $message) :
            ?>
            <li><?= $message ?></li>
        <?php
        endforeach
        ?>
    </ul>
</div>
<div class="admin-search-n-filter">
    <div class="admin-search">
        <label><input type="text" value="<?= $this->enteredKeyWords ?>"/></label>
        <input type="button" value=""/>
    </div>
    <div class="admin-hyperlink-btn">
        <a href="/classico/public/admin/comment/index">Refresh</a>
    </div>
    <div class="admin-filter">
        <label>
            <select>
                <?php
                if (!isset($this->confirmed)) :
                    ?>
                    <option value="null">-- Confirmed --</option>
                    <option value="1">True</option>
                    <option value="0">False</option>
                <?php
                endif;
                if ($this->confirmed === true) :
                    ?>
                    <option value="null">-- Confirmed --</option>
                    <option value="1" selected="selected">True</option>
                    <option value="0">False</option>
                <?php
                endif;
                if ($this->confirmed === false) :
                    ?>
                    <option value="null">-- Confirmed --</option>
                    <option value="1">True</option>
                    <option value="0" selected="selected">False</option>
                <?php
                endif;
                ?>
            </select>
        </label>
    </div>
</div>
<div class="datagrid">
    <table>
        <tr>
            <th width="2%"><label><input id="check-box-all" type="checkbox"/></label></th>
            <th width="2%">ID</th>
            <th>Content</th>
            <th width="12%">Author</th>
            <th width="12%">CreatedDate</th>
            <th width="5%">ProductID</th>
            <th width="5%">UserID</th>
            <th width="5%" class="center">Confirmed</th>
            <th width="5%" class="center">Deleted</th>
        </tr>
        <?php
        $comments = $this->comments;
        foreach ($comments as $index => $comment) :
            ?>
            <tr class="<?= ($index % 2 == 0) ? 'alt' : '' ?>">
                <td>
                    <label>
                        <input class="check-box-comment-id" type="checkbox"
                               value="<?= $comment['ID'] ?>"/>
                    </label>
                </td>
                <td><?= $comment['ID'] ?></td>
                <td><?= $comment['Content'] ?></td>
                <td><?= $comment['Author'] ?></td>
                <td><?= $comment['CreatedDate'] ?></td>
                <td><?= $comment['ProductID'] ?></td>
                <td><?= $comment['UserID'] ?></td>
                <td class="center">
                    <?php
                    if (!$comment['Confirmed']) :
                        ?>
                        <div class="enabled-block-icon tool-icon"></div>
                        <form action="/classico/public/admin/comment/confirm"
                              method="post">
                            <input type="hidden" name="isPost" value=""/>
                            <input type="hidden" name="commentIDs[]"
                                   value="<?= $comment['ID'] ?>"/>
                        </form>
                    <?php
                    endif;
                    if ($comment['Confirmed']) :
                        ?>
                        <div class="enabled-confirm-icon tool-icon"></div>
                        <form action="/classico/public/admin/comment/unconfirm"
                              method="post">
                            <input type="hidden" name="isPost" value=""/>
                            <input type="hidden" name="commentID"
                                   value="<?= $comment['ID'] ?>"/>
                        </form>
                    <?php
                    endif
                    ?>
                </td>
                <td class="center">
                    <div class="enabled-delete-icon tool-icon"></div>
                    <form action="/classico/public/admin/comment/delete" method="post">
                        <input type="hidden" name="isPost" value=""/>
                        <input type="hidden" name="commentIDs[]"
                               value="<?= $comment['ID'] ?>"/>
                    </form>
                </td>
            </tr>
        <?php
        endforeach
        ?>
    </table>
</div>
<?= $this->paginationControl($this->paginator, 'Sliding', 'my_pagination_control.phtml') ?>
<script type="text/javascript">
    $('#tool-btn-add, #tool-btn-update, #tool-btn-unblock, #tool-btn-block')
        .removeClass('enabled-tool-btn');

    $('#tool-btn-add > div')
        .removeClass('enabled-add-icon')
        .addClass('disabled-add-icon');
    $('#tool-btn-update > div')
        .removeClass('enabled-update-icon')
        .addClass('disabled-update-icon');
    $('#tool-btn-unblock > div')
        .removeClass('enabled-unblock-icon')
        .addClass('disabled-unblock-icon');
    $('#tool-btn-block > div')
        .removeClass('enabled-block-icon')
        .addClass('disabled-block-icon');
</script>
<script type="text/javascript">
    $('#check-box-all').click(function () {
        if ($(this).attr('checked')) {
            $('.check-box-comment-id').attr('checked', true);
        } else {
            $('.check-box-comment-id').attr('checked', false);
        }
    });

    $('.datagrid .tool-icon').click(function () {
        $(this).parent(null).find('form').submit();
    });
</script>
<script type="text/javascript">
    function forwardHyperlink() {
        var currentURL = location.href;

        var forwardURL = currentURL.replace(/\/comment\/[\w\W]*/, '');
        forwardURL = forwardURL.replace(/\/index\/[\w\W]*/, '');
        forwardURL = forwardURL.replace(/\/keyWords\/[\w\W]*/, '');
        forwardURL = forwardURL.replace(/\/confirmed\/[\w\W]*/, '');
        forwardURL = forwardURL.replace(/\/page\/[\w\W]*/, '');

        forwardURL += '/comment/index';

        var keyWords = $('.admin-search input[type = "text"]').val();
        if (keyWords) {
            forwardURL += '/keyWords/' + keyWords;
        }

        var confirmedValue = $('.admin-filter select').val();
        if (confirmedValue != 'null') {
            forwardURL += '/confirmed/' + confirmedValue;
        }

        window.location.href = forwardURL;
    }

    $('.admin-search input[type = "text"]').keydown(function (event) {
        if (event.which == 13) {
            forwardHyperlink();
        }
    });

    $('.admin-search input[type = "button"]').click(function () {
        forwardHyperlink();
    });

    $('.admin-filter select').change(function () {
        forwardHyperlink();
    });
</script>
<script type="text/javascript">
    function process() {
        var form = $(this).find('form');

        var checkedCheckBoxes = $('.check-box-comment-id:checked');
        for (var i = 0; i < checkedCheckBoxes.length; i++) {
            var commentID = checkedCheckBoxes[i].value;
            var tag = '<input type="hidden" name="commentIDs[]" value="' + commentID + '"/>';
            form.append(tag);
        }

        form.submit();
    }

    $('#tool-btn-delete.enabled-tool-btn').click(process);
    $('#tool-btn-confirm.enabled-tool-btn').click(process);
</script>
